/***********************************************
 @Name :        小铭同学
 @Time :        2025-08-15
 @Email :       LiaoMingWJ233@163.com
 @Comment :     功能窗口 工业控制
 @FileName :    widget_funce_control_industrial.cpp
 @Version :     1.0
************************************************/


#include "widget_funce_control_industrial.h"
#include "ui_widget_funce_control_industrial.h"

Widget_funcE_Control_industrial::Widget_funcE_Control_industrial(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget_funcE_Control_industrial)
{
    ui->setupUi(this);
    
    ui->label->setText("TCP 通讯");
    ui->label->setFont(QFont("楷体",20));
    ui->label->setStyleSheet("color: #e056c4;");
    
    ui->pushButton_lj->setIcon(QIcon(":/连接按钮"));
    ui->pushButton_lj->setText("");
    ui->pushButton_lj->setIconSize(QSize(80, 35));
    ui->pushButton_lj->setFlat(true);  // 移除默认背景
    ui->pushButton_lj->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    ); 
    
    ui->pushButton_fs->setIcon(QIcon(":/发送按钮"));
    ui->pushButton_fs->setText("");
    ui->pushButton_fs->setIconSize(QSize(80, 35));
    ui->pushButton_fs->setFlat(true);  // 移除默认背景
    ui->pushButton_fs->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    );  
    
    connect(ui->pushButton_52 , &QPushButton::clicked , this , [&]()
    {
        ui->pushButton_52->setIcon(QIcon(":/向下_开"));
        TCP.TCP_send("胶桶温度 -5℃\n");        
    });
    
    connect(ui->pushButton_51 , &QPushButton::clicked , this , [&]()
    {
        ui->pushButton_51->setIcon(QIcon(":/向上_开"));
        TCP.TCP_send("胶桶温度 +5℃\n");
    });
    
    connect(ui->pushButton_42 , &QPushButton::clicked , this , [&]()
    {       
        ui->pushButton_42->setIcon(QIcon(":/向下_开"));
        TCP.TCP_send("Z轴 -5 \n");
    });
    
    connect(ui->pushButton_41 , &QPushButton::clicked , this , [&]()
    {        
        ui->pushButton_41->setIcon(QIcon(":/向上_开"));
        TCP.TCP_send("Z轴 +5 \n");           
    });
    
    connect(ui->pushButton_32 , &QPushButton::clicked , this , [&]()
    {
        ui->pushButton_32->setIcon(QIcon(":/向下_开"));
        TCP.TCP_send("Y轴 -5 \n");          
    });
    
    connect(ui->pushButton_31 , &QPushButton::clicked , this , [&]()
    {        
        ui->pushButton_31->setIcon(QIcon(":/向上_开"));
        TCP.TCP_send("Y轴 +5 \n");
    });
    
    connect(ui->pushButton_22 , &QPushButton::clicked , this , [&]()
    {        
        ui->pushButton_22->setIcon(QIcon(":/向下_开"));
        TCP.TCP_send("X轴 -5 \n");          
    });
    
    connect(ui->pushButton_21 , &QPushButton::clicked , this , [&]()
    {        
        ui->pushButton_21->setIcon(QIcon(":/向上_开"));
        TCP.TCP_send("X轴 +5 \n");
    });
    
    connect(ui->pushButton_12 , &QPushButton::clicked , this , [&]()
    {        
        ui->pushButton_12->setIcon(QIcon(":/向下_开"));
        TCP.TCP_send("针头温度 -5℃\n");            
    });
    
    connect(ui->pushButton_11 , &QPushButton::clicked , this , [&]()
    {        
        ui->pushButton_11->setIcon(QIcon(":/向上_开"));
        TCP.TCP_send("针头温度 +5℃\n");           
    });
    
    ui->pushButton_51->setIcon(QIcon(":/向上_关"));
    ui->pushButton_51->setText("");
    ui->pushButton_51->setIconSize(QSize(45, 37));
    ui->pushButton_51->setFlat(true);  // 移除默认背景
    ui->pushButton_51->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    ); 
    
    ui->pushButton_52->setIcon(QIcon(":/向下_关"));
    ui->pushButton_52->setText("");
    ui->pushButton_52->setIconSize(QSize(45, 37));
    ui->pushButton_52->setFlat(true);  // 移除默认背景
    ui->pushButton_52->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    ); 
    
    ui->pushButton_41->setIcon(QIcon(":/向上_关"));
    ui->pushButton_41->setText("");
    ui->pushButton_41->setIconSize(QSize(56, 37));
    ui->pushButton_41->setFlat(true);  // 移除默认背景
    ui->pushButton_41->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    ); 
    
    ui->pushButton_42->setIcon(QIcon(":/向下_关"));
    ui->pushButton_42->setText("");
    ui->pushButton_42->setIconSize(QSize(56, 37));
    ui->pushButton_42->setFlat(true);  // 移除默认背景
    ui->pushButton_42->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    ); 
    
    ui->pushButton_31->setIcon(QIcon(":/向上_关"));
    ui->pushButton_31->setText("");
    ui->pushButton_31->setIconSize(QSize(56, 37));
    ui->pushButton_31->setFlat(true);  // 移除默认背景
    ui->pushButton_31->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    ); 
    
    ui->pushButton_32->setIcon(QIcon(":/向下_关"));
    ui->pushButton_32->setText("");
    ui->pushButton_32->setIconSize(QSize(56, 37));
    ui->pushButton_32->setFlat(true);  // 移除默认背景
    ui->pushButton_32->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    ); 
    
    ui->pushButton_21->setIcon(QIcon(":/向上_关"));
    ui->pushButton_21->setText("");
    ui->pushButton_21->setIconSize(QSize(56, 37));
    ui->pushButton_21->setFlat(true);  // 移除默认背景
    ui->pushButton_21->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    ); 
    
    ui->pushButton_22->setIcon(QIcon(":/向下_关"));
    ui->pushButton_22->setText("");
    ui->pushButton_22->setIconSize(QSize(56, 37));
    ui->pushButton_22->setFlat(true);  // 移除默认背景
    ui->pushButton_22->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    ); 
    
    ui->pushButton_11->setIcon(QIcon(":/向上_关"));
    ui->pushButton_11->setText("");
    ui->pushButton_11->setIconSize(QSize(45, 37));
    ui->pushButton_11->setFlat(true);  // 移除默认背景
    ui->pushButton_11->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    ); 
    
    ui->pushButton_12->setIcon(QIcon(":/向下_关"));
    ui->pushButton_12->setText("");
    ui->pushButton_12->setIconSize(QSize(45, 37));
    ui->pushButton_12->setFlat(true);  // 移除默认背景
    ui->pushButton_12->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    ); 
    
    
    
    ui->label_11->setText("针头");
    ui->label_11->setFont(QFont("楷体",15));
    ui->label_11->setStyleSheet("color: #e056c4;");
    
    ui->label_21->setText("X");
    ui->label_21->setFont(QFont("楷体",15));
    ui->label_21->setStyleSheet("color: #e056c4;");
    
    ui->label_31->setText("Y");
    ui->label_31->setFont(QFont("楷体",15));
    ui->label_31->setStyleSheet("color: #e056c4;");
    
    ui->label_41->setText("Z");
    ui->label_41->setFont(QFont("楷体",15));
    ui->label_41->setStyleSheet("color: #e056c4;");
    
    ui->label_51->setText("胶桶");
    ui->label_51->setFont(QFont("楷体",15));
    ui->label_51->setStyleSheet("color: #e056c4;");
    
    
    ui->horizontalSlider->setStyleSheet(
        // 滑块容器设置
        "QSlider {"
        "    min-width: 200px;"
        "    max-width: 200px;"
        "    min-height: 12px;"  // 容器最小高度
        "}"
        
        // 轨道样式（极细3px）- 主背景层
        "QSlider::groove:horizontal {"
        "    border: 1px solid #d9d9d9;"
        "    background: white;"
        "    height: 10px;"
        "    border-radius: 1.5px;"
        "    margin: 4px 0;"
        "}"
        
        // 进度条（蓝紫渐变）- 严格匹配轨道尺寸
        "QSlider::sub-page:horizontal {"
        "    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "                               stop:0 #8a2be2, stop:1 #00d4ff);"  // 紫到荧光蓝
        "    height: 3px;"       // 严格匹配轨道高度
        "    margin-top: 4px;"   // 与轨道margin同步
        "    margin-bottom: 4px;"
        "    border-radius: 1.5px;"
        "}"
        
        // 未滑过部分（透明且不干扰布局）
        "QSlider::add-page:horizontal {"
        "    background: transparent;"
        "    height: 0px;"       // 避免产生额外空间
        "    margin: 0;"
        "}"
        
        // 手柄（8px圆形，精确垂直居中）
        "QSlider::handle:horizontal {"
        "    background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
        "                               stop:0 #2ecc71, stop:1 #27ae60);"
        "    border: 1px solid #27ae60;"
        "    width: 8px;"
        "    height: 8px;"
        "    margin: -3px 0;"    // 计算公式：(3+4+4-8)/2 = -3
        "    border-radius: 4px;"
        "}"
        
        // 悬停状态（手柄轻微放大）
        "QSlider::handle:horizontal:hover {"
        "    background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
        "                               stop:0 #5efc82, stop:1 #2ecc71);"
        "    width: 10px;"
        "    height: 10px;"
        "    margin: -4px 0;"     // 重新计算居中：(3+4+4-10)/2 = -4
        "}"
    );
    
    ui->label_hd2->setText("0.01mm");
    ui->label_hd2->setFont(QFont("楷体",8));
    ui->label_hd2->setStyleSheet("color: #e056c4;");
    
    ui->label_hd1->setText("电机\n精度");
    ui->label_hd1->setFont(QFont("楷体",10));
    ui->label_hd1->setStyleSheet("color: #e056c4;");
    
    ui->label_jiarwd->setText("加热\n温度\n230℃");
    ui->label_jiarwd->setFont(QFont("楷体",15));
    ui->label_jiarwd->setStyleSheet("color: #e056c4;");
    
    ui->label_jchuj->setText("针头\n温度\n250℃");
    ui->label_jchuj->setFont(QFont("楷体",15));
    ui->label_jchuj->setStyleSheet("color: #e056c4;");
    
    ui->label_jtwd->setText("胶桶\n温度\n230℃");
    ui->label_jtwd->setFont(QFont("楷体",15));
    ui->label_jtwd->setStyleSheet("color: #e056c4;");
    
    ui->label_jgjd->setText("加工\n进度\n90%");
    ui->label_jgjd->setFont(QFont("楷体",15));
    ui->label_jgjd->setStyleSheet("color: #e056c4;");
    
    connect(ui->pushButton_zhaomd , &QPushButton::clicked , this , [&]()
    {
        if(isEmergencyStopped7) {
                ui->pushButton_zhaomd->setIcon(QIcon(":/滑动开关_关"));
                TCP.TCP_send("照明灯_关\n");
            } else {
                ui->pushButton_zhaomd->setIcon(QIcon(":/滑动开关_开"));
                TCP.TCP_send("照明灯_开\n");
            }
            isEmergencyStopped7 = !isEmergencyStopped7; // 切换状态
    });
    
    connect(ui->pushButton_jiar , &QPushButton::clicked , this , [&]()
    {
        if(isEmergencyStopped6) {
                ui->pushButton_jiar->setIcon(QIcon(":/滑动开关_关"));
                TCP.TCP_send("加热_关\n");
            } else {
                ui->pushButton_jiar->setIcon(QIcon(":/滑动开关_开"));
                TCP.TCP_send("加热_开\n");
            }
            isEmergencyStopped6 = !isEmergencyStopped6; // 切换状态
    });
    
    connect(ui->pushButton_chuj , &QPushButton::clicked , this , [&]()
    {
        if(isEmergencyStopped5) {
                ui->pushButton_chuj->setIcon(QIcon(":/滑动开关_关"));
                TCP.TCP_send("出胶_关\n");
            } else {
                ui->pushButton_chuj->setIcon(QIcon(":/滑动开关_开"));
                TCP.TCP_send("出胶_开\n");
            }
            isEmergencyStopped5 = !isEmergencyStopped5; // 切换状态
    });
    
    connect(ui->pushButton_fengs , &QPushButton::clicked , this , [&]()
    {
        if(isEmergencyStopped4) {
                ui->pushButton_fengs->setIcon(QIcon(":/滑动开关_关"));
                TCP.TCP_send("风扇_关\n");
            } else {
                ui->pushButton_fengs->setIcon(QIcon(":/滑动开关_开"));
                TCP.TCP_send("风扇_开\n");
            }
            isEmergencyStopped4 = !isEmergencyStopped4; // 切换状态
    });
    
    ui->label_fengs->setText("冷却\n风扇");
    ui->label_fengs->setFont(QFont("楷体",15));
    ui->label_fengs->setStyleSheet("color: #e056c4;");
    
    ui->label_zhaomd->setText("照明灯");
    ui->label_zhaomd->setFont(QFont("楷体",15));
    ui->label_zhaomd->setStyleSheet("color: #e056c4;");
    
    ui->label_jiar->setText("加热");
    ui->label_jiar->setFont(QFont("楷体",15));
    ui->label_jiar->setStyleSheet("color: #e056c4;");
    
    ui->label_chuj->setText("出胶");
    ui->label_chuj->setFont(QFont("楷体",15));
    ui->label_chuj->setStyleSheet("color: #e056c4;");
    
    ui->pushButton_fengs->setIcon(QIcon(":/滑动开关_关"));
    ui->pushButton_fengs->setText("");
    ui->pushButton_fengs->setIconSize(QSize(68, 30));
    ui->pushButton_fengs->setFlat(true);  // 移除默认背景
    ui->pushButton_fengs->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    ); 
    
    ui->pushButton_zhaomd->setIcon(QIcon(":/滑动开关_关"));
    ui->pushButton_zhaomd->setText("");
    ui->pushButton_zhaomd->setIconSize(QSize(68, 30));
    ui->pushButton_zhaomd->setFlat(true);  // 移除默认背景
    ui->pushButton_zhaomd->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    );
    
    ui->pushButton_jiar->setIcon(QIcon(":/滑动开关_关"));
    ui->pushButton_jiar->setText("");
    ui->pushButton_jiar->setIconSize(QSize(68, 30));
    ui->pushButton_jiar->setFlat(true);  // 移除默认背景
    ui->pushButton_jiar->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    );
    
    ui->pushButton_chuj->setIcon(QIcon(":/滑动开关_关"));
    ui->pushButton_chuj->setText("");
    ui->pushButton_chuj->setIconSize(QSize(68, 30));
    ui->pushButton_chuj->setFlat(true);  // 移除默认背景
    ui->pushButton_chuj->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    );
      
    connect(ui->pushButton_jt , &QPushButton::clicked , this , [&]()
    {
        if(isEmergencyStopped1) {
                ui->pushButton_jt->setIcon(QIcon(":/急停_关"));
                TCP.TCP_send("急停_关\n");
            } else {
                ui->pushButton_jt->setIcon(QIcon(":/急停_开"));
                TCP.TCP_send("急停_开\n");
            }
            isEmergencyStopped1 = !isEmergencyStopped1; // 切换状态
    });
    
    connect(ui->pushButton_tz , &QPushButton::clicked , this , [&]()
    {
        if(isEmergencyStopped2) {
                ui->pushButton_tz->setIcon(QIcon(":/停止_关"));
                TCP.TCP_send("停止_关\n");
            } else {
                ui->pushButton_tz->setIcon(QIcon(":/停止_开"));
                TCP.TCP_send("停止_开\n");
            }
            isEmergencyStopped2 = !isEmergencyStopped2; // 切换状态
    });
    
    connect(ui->pushButton_gj , &QPushButton::clicked , this , [&]()
    {
        if(isEmergencyStopped3) {
                ui->pushButton_gj->setIcon(QIcon(":/关机_关"));
                TCP.TCP_send("关机_关\n");
            } else {
                ui->pushButton_gj->setIcon(QIcon(":/关机_开"));
                TCP.TCP_send("关机_开\n");
            }
            isEmergencyStopped3 = !isEmergencyStopped3; // 切换状态
    });
    
    connect(ui->pushButton_hj , &QPushButton::clicked , this , [&]()
    {
        if(isEmergencyStopped4) {
                ui->pushButton_hj->setIcon(QIcon(":/换胶_关"));
                TCP.TCP_send("换胶_关\n");
            } else {
                ui->pushButton_hj->setIcon(QIcon(":/换胶_开"));
                TCP.TCP_send("换胶_开\n");
            }
            isEmergencyStopped4 = !isEmergencyStopped4; // 切换状态
    });
    
    // 点胶机 4个控制按钮
    ui->pushButton_jt->setIcon(QIcon(":/急停_关"));
    ui->pushButton_jt->setText("");
    ui->pushButton_jt->setIconSize(QSize(60, 60));
    ui->pushButton_jt->setFlat(true);  // 移除默认背景
    ui->pushButton_jt->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    ); 
    
    ui->pushButton_tz->setIcon(QIcon(":/停止_关"));
    ui->pushButton_tz->setText("");
    ui->pushButton_tz->setIconSize(QSize(60, 60));
    ui->pushButton_tz->setFlat(true);  // 移除默认背景
    ui->pushButton_tz->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    );
    
    ui->pushButton_gj->setIcon(QIcon(":/关机_关"));
    ui->pushButton_gj->setText("");
    ui->pushButton_gj->setIconSize(QSize(60, 60));
    ui->pushButton_gj->setFlat(true);  // 移除默认背景
    ui->pushButton_gj->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    ); 
    
    ui->pushButton_hj->setIcon(QIcon(":/换胶_关"));
    ui->pushButton_hj->setText("");
    ui->pushButton_hj->setIconSize(QSize(60, 60));
    ui->pushButton_hj->setFlat(true);  // 移除默认背景
    ui->pushButton_hj->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    );  
       
    ui->label_glue_dispenser->setPixmap(QPixmap(":/点胶机"));
    
    ui->label_glue_dispenser_bt->setText("全自动视觉点胶机");
    ui->label_glue_dispenser_bt->setFont(QFont("楷体",30));
    ui->label_glue_dispenser_bt->setStyleSheet("color: #e056c4;");
    
    ui->label_js->setFont(QFont("楷体",15));
    ui->label_js->setStyleSheet("color: #e056c4;");
    
    // 最重要的信号，当有新数据可读时触发
    connect(TCP.TcpSocket, &QTcpSocket::readyRead, this, &Widget_funcE_Control_industrial::readData);
}

Widget_funcE_Control_industrial::~Widget_funcE_Control_industrial()
{
    delete ui;
}

void Widget_funcE_Control_industrial::on_pushButton_lj_clicked()
{
    TCP.TCP_connect();
}

void Widget_funcE_Control_industrial::on_pushButton_fs_clicked()
{
    TCP.TCP_send(ui->textEdit_fs->toPlainText()+"\n");
    qDebug() << ui->textEdit_fs->toPlainText();
    ui->textEdit_fs->clear();
}

void Widget_funcE_Control_industrial::readData()
{
    QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
    if (!socket)
        return;
    
    ui->label_js->clear();
    QString data = socket->readAll();        
        
    ui->label_js->setText(data);
    
    qDebug() << data;
}
